package com.lean.demo.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.lean.demo.entity.Area;
import com.lean.demo.service.AreaService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class Controller {
    @Autowired
    private AreaService areaService;

    @GetMapping("/queryById/{areaId}")
    public Map<String,Object> queryById(@PathVariable("areaId") Integer areaId){
       Area area =  areaService.queryAreaById(areaId);
        Map<String,Object> modelMap = new HashMap<String,Object>();
        modelMap.put("area",area);
//        int i = 1/0;
        return modelMap;
    }
    @GetMapping("/list")
    public Map<String,Object> list(){
        List<Area> areas =  areaService.queryAreas();
        Map<String,Object> modelMap = new HashMap<String,Object>();
        modelMap.put("areas",areas);
        return modelMap;
    }

    @PostMapping("/addArea")
    public Map<String,Object> add(@RequestBody Area area){

        Map<String,Object> modelMap = new HashMap<>();
        modelMap.put("success",areaService.addArea(area));
        return modelMap;
    }

    @PostMapping("/update")
    public Map<String,Object> update(Area area){
        Map<String,Object> modelMap = new HashMap<>();
        modelMap.put("success",areaService.editArea(area));
        return modelMap;
    }
    @GetMapping("/deleteAreaById")
    public Map<String,Object> delete( Integer areaId){
        Map<String,Object> modelMap = new HashMap<String,Object>();
        modelMap.put("success",areaService.deleteAreaById(areaId));
        return modelMap;
    }

}
